#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
主题: 同时迭代多个序列
问题: 你想同时迭代多个序列，每次分别从一个序列中取一个元素。
提示 : 
    1. zip() 会创建一个迭代器来作为结果返回。 如果你需要将结对的值存储在列表中，要使用 list() 函数
"""


def recipe1():
    """同时迭代多个序列，使用 zip() 函数: 迭代长度跟参数中最短序列长度一致"""
    xpts = [1, 5, 4, 2, 10, 7]
    ypts = [101, 78, 37, 15, 62, 99, 0]
    for x, y in zip(xpts, ypts):
        print(f"{x, y = }")

    from itertools import zip_longest
    for x1, y1 in zip_longest(xpts, ypts):
        print(f"{x1, y1 = }")


def recipe2():
    """头列表和一个值列表"""
    headers = ['name', 'shares', 'price']
    values = ['ACME', 100, 490.1]

    s = dict(zip(headers, values))
    print(f"{s = }")

    for name, val in zip(headers, values):
        print(f"{name, '=', val = }")


def recipe3():
    """接受多于两个的序列的参数"""
    a = [1, 2, 3]
    b = [10, 11, 12]
    c = ['x','y','z']
    for i in zip(a, b, c):
        print(f"{i = }")

def main():
    print('recipe1'.center(20, '*'))
    recipe1()
    print('recipe2'.center(20, '*'))
    recipe2()
    print('recipe3'.center(20, '*'))
    recipe3()

if __name__ == '__main__':
    main()
